<?php

namespace App\V3\V3_1\Controllers;

use App\V3\V3_1\Models\VersionsModel;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class VersionsController extends Controller
{
    /**
     * 列表
     * change 在 2021/10/25 15:39 创建
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        $model = VersionsModel::orderBy('id','desc')
            ->simplePaginate($data['pageSize'],['*'], '',$data['currentPage']);
        return success($model);
    }

    /**
     * 最新版本
     * change 在 2021/12/16 10:37 创建
     * @return \Illuminate\Http\JsonResponse
     */
    public function find(){
        $model = VersionsModel::select('version_num','url','url_one')->orderBy('version_num','desc')->first();
        return success($model);
    }

    /**
     * 添加
     * change 在 2021/10/25 15:39 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'version_num' =>'string',//版本号
            'url' =>'required|string',//下载链接
            'url_one' =>'required|string',//下载链接2
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        $data['created_at'] = time();
        VersionsModel::insert($data);
        return success();
    }

    /**
     * 编辑
     * change 在 2021/10/25 15:39 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' =>'required|integer',//企业类型主键id
            'version_num' =>'string',//版本号
            'url' =>'string',//下载链接
            'url_one' =>'string',//下载链接2
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        VersionsModel::where('id',$data['id'])->update($data);
        return success();
    }

    /**
     * 删除
     * change 在 2021/10/25 15:40 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function del(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' =>'required|integer',//企业类型主键id
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        VersionsModel::destroy($data['id']);
        return success();
    }
}
